/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gr.uop.cst.context.adaptation.aspectj;

import gr.uop.cst.context.adaptation.artifacts.oom.AbstractMemberAdaptation;
import gr.uop.cst.context.adaptation.Action;
import gr.uop.cst.context.adaptation.ContextAdaptation;
import gr.uop.cst.context.adaptation.ContextRule;
import java.io.Serializable;
import java.lang.reflect.Member;
import java.util.HashMap;

/**
 *
 * @author jbourlakos
 */
public class AspectJContextLookup implements Serializable {
    
    private HashMap<String,ContextRule> pointcutToRule = new HashMap<String,ContextRule>();
    private HashMap<String,Action> pointcutToAction = new HashMap<String,Action>();
    
    
    public AspectJContextLookup() {
        
    }
    
    
    public static String generatePointcutName(AbstractMemberAdaptation ama) {
        Member m;
        try {
            m = ama.getMember();
        } catch (Exception ex) {
            throw new RuntimeException(ex); //TODO
        }
        String className = m.getDeclaringClass().getCanonicalName();
        String memberName = m.getName();
        String pointcutName =  ama.getClass().getSimpleName() + "_" + className + "_" + memberName;
        return pointcutName;
    }
    
    
    public void register(ContextRule r) {
        ContextAdaptation ca = r.getAdaptation();
        AbstractMemberAdaptation ama;
        try {
            ama = (AbstractMemberAdaptation) ca;
        } catch(ClassCastException ex) {
            throw new UnsupportedOperationException("Unsupported adaptation type: " + ca.getClass().getCanonicalName());
        }
        String key = generatePointcutName(ama);
        pointcutToRule.put(key, r);
        pointcutToAction.put(key,ama.getAction());
    }
    
    public Action getActionFor(String pc) {
        return pointcutToAction.get(pc);
    }
    
    public ContextRule getRuleFor(String pc) {
        return pointcutToRule.get(pc);
    }
    
}
